# coding=utf-8
"""
互斥锁典型案例
"""
from threading import Thread, Lock
from time import sleep


class Account:
    def __init__(self, money, name):
        self.money = money
        self.name = name


# 模拟提款操作
class Drawing(Thread):
    def __init__(self, drawingNum, account):
        Thread.__init__(self)
        self.drawingNum = drawingNum
        self.account = account
        self.expenseTotal = 0

    def run(self):
        lock1.acquire()
        if self.account.money < self.drawingNum:
            print("账户余额不足")
            return
        sleep(1)  # 判断完后阻塞
        self.account.money -= self.drawingNum
        self.expenseTotal += self.drawingNum
        lock1.release()
        print(f"账户{self.account.name}，余额{self.account.money}")
        print(f"账户{self.account.name}，总共取了{self.expenseTotal}")


if __name__ == '__main__':
    a1 = Account(100, "gaoqi")
    lock1 = Lock()
    draw1 = Drawing(80, a1)  # 定义取钱线程对象；
    draw2 = Drawing(80, a1)  # 定义取钱线程对象；
    draw1.start()  # 你取钱
    draw2.start()  # 你老婆取钱
